package collector.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import util.IOUtils;

public class Util {

    /**
     * 根据url获取html内容
     * @param url
     * @return String
     * @author 李东喜
     * 2009-3-31
     */
    public static String getHtml(String url) {
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        InputStreamReader isr = null;

        try {
            if (isNotEmpty(url)) {
                URL u = new URL(url);
                is = u.openStream();
                isr = new InputStreamReader(is);
                int length = -1;
                char[] chars = new char[1024 * 8];
                while (-1 != (length = isr.read(chars))) {
                    sb.append(new String(chars, 0, length));
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(isr);
            IOUtils.close(is);
        }
        
        return sb.toString().trim();
    }
    
    /**
     * 根据正则表达式解析字符串，并返回第一个匹配的字符串
     * @param s
     * @param reg 正则表达式
     * @return List&lt;String&gt;
     * @author 李东喜
     * 2009-4-17
     */
    public static String getMatchedString(String s, String reg) {
        
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
    
    /**
     * 根据正则表达式解析字符串，并返回匹配的字符列表
     * @param s
     * @param reg 正则表达式
     * @return List&lt;String&gt;
     * @author 李东喜
     * 2009-4-17
     */
    public static List<String> getMatchedStrings(String s, String reg) {
        
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        List<String> l = new ArrayList<String>();
        while (matcher.find()) {
            l.add(matcher.group());
            //System.out.println(matcher.group());
        }
        return l;
    }
    
    /**
     * 根据url和&lt;a&gt;里面的href，返回一个绝对的http://地址<br>
     * &lt;a&gt;里面的href的形式：http://, ../, ./, others
     * @param url
     * @param link
     * @return String
     * @author 李东喜
     * 2009-4-20
     */
    public static String processLink(String url, String link) {
        String _link = null;
        
        if (link.startsWith("http://")) {
            _link = link;
        } else if (link.startsWith("../")) {
            while (link.startsWith("../")) {
                link = link.replaceFirst("\\.\\./", "");
                url = url.substring(0, url.lastIndexOf('/', url.lastIndexOf('/') - 1) + 1);
            }
            _link = url + link;
        } else if (link.startsWith("./")) {
            _link = url.substring(0, url.lastIndexOf('/') + 1) + link.substring(2);
        } else if (link.startsWith("/")) {
            String host = url.substring(0, url.indexOf('/', "http://".length()));
            _link = host + link;
        } else {
            _link = url.substring(0, url.lastIndexOf('/') + 1) + link;
        }
        
        return _link;
    }

    /**
     * 判断是否为null或空字符串
     * @param s
     * @return boolean
     * @author 李东喜
     * 2009-4-17
     */
    public static boolean isEmpty(String s) {
        return null == s || "".equals(s.trim());
    }
    
    /**
     * 判断是否为null或空字符串
     * @param s
     * @return boolean
     * @author 李东喜
     * 2009-4-17
     */
    public static boolean isNotEmpty(String s) {
        return !isEmpty(s);
    }
    
    public static void main(String[] args) {
        
    }
}
